<?php
class ThoigiansController extends AppController {

	var $name = 'Thoigians';

	function index() {
		$this->Thoigian->recursive = 0;
		$this->set('thoigians', $this->paginate());
	}
    /**
     * Danh cho chi nhanh, và vùng
     * */
    function indexcn() {
		$this->Thoigian->recursive = 0;
		$this->set('thoigians', $this->paginate());
	}
    function themchitieu($donvi=null) {
		$this->Thoigian->recursive = 0;
		$thang=$this->Thoigian->query(" select thoigians.id, thoigians.tentg, thoigians.ngaybd, thoigians.ngaykt
                                    from thoigians
                                    where DATEDIFF(thoigians.ngaykt,thoigians.ngaybd)>8
                                    and thoigians.id NOT IN (select thoigian_id from kehoaches )
                                   ");
        $this->set(compact('thang','donvi'));
        //print_r($thang);
        //print_r($donvi);
	}
    
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid thoigian', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('thoigian', $this->Thoigian->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
		  $this->data['Thoigian']['ngaybd'] = $_POST['ngaybd'];
             $this->data['Thoigian']['ngaykt'] = $_POST['ngaykt'];
             $bd=$this->Thoigian->query("select dayofweek('".
                        $this->data['Thoigian']['ngaybd']."') as bd");
             $kt=$this->Thoigian->query("select dayofweek('".
                        $this->data['Thoigian']['ngaykt']."') as kt");
              //  print_r($bd);
             if($bd[0][0]['bd']!=2){
                    $this->Session->setFlash(__('Ngày bắt đầu phải là thứ 2!', true));
                    
   		       
             }
             else if($kt[0][0]['kt']!=1){
                    $this->Session->setFlash(__('Ngày kết thúc 
                                phải là chủ nhật', true));
             }
			$this->Thoigian->create();
			if ($this->Thoigian->save($this->data)) {
				$this->Session->setFlash(__('The thoigian has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thoigian could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid thoigian', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
		  $this->data['Thoigian']['ngaybd'] = $_POST['ngaybd'];
             $this->data['Thoigian']['ngaykt'] = $_POST['ngaykt'];
             $bd=$this->Thoigian->query("select dayofweek('".
                        $this->data['Thoigian']['ngaybd']."') as bd");
             $kt=$this->Thoigian->query("select dayofweek('".
                        $this->data['Thoigian']['ngaykt']."') as kt");
              //  print_r($bd);
             if($bd[0][0]['bd']!=2){
                    $this->Session->setFlash(__('Ngày bắt đầu HK phải là thứ 2!', true));
                    
   		       
             }
             else if($kt[0][0]['kt']!=1){
                    $this->Session->setFlash(__('Ngày kết thúc 
                                HK phải là chủ nhật', true));
             }
			if ($this->Thoigian->save($this->data)) {
				$this->Session->setFlash(__('The thoigian has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The thoigian could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Thoigian->read(null, $id);
            $this->set('time', $this->data['Thoigian']);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for thoigian', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Thoigian->delete($id)) {
			$this->Session->setFlash(__('Thoigian deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Thoigian was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
